<template>
  <div class="roport_container">
    <el-card>
      <!-- 为 ECharts 准备一个具备大小（宽高）的 DOM -->
      <div
        id="yonghu"
        :style="{width: '100%',
            height: '300px',
            throttle: 70, left: 'center', top: 'middle'}"
      ></div>

      <div
        id="dianji"
        :style="{width: '100%',
            height: '300px',
            throttle: 70, left: 'center', top: '30px'}"
        style="margin-top: 20px"
      ></div>
    </el-card>
  </div>
</template>

<script>
import echarts from 'echarts'
export default {
  data() {
    return {
      // 需要合并的数据报表
      option1: {
        backgroundColor: 'rgb(170, 170, 170)',
        title: {
          text: '用户来源'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEA3'
            }
          }
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            boundaryGap: false,
            axisLabel: {
              show: true,
              textStyle: {
                color: 'green'
              }
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: 'green'
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisLabel: {
              show: true,
              textStyle: {
                color: 'orange'
              }
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: 'orange'
              }
            }
          }
        ]
      },
      // 单折线 图
      option2: {
        title: {
          text: '访问量分布'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['首页', '分类', '商品列表', '商品详情']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: []
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '首页',
            type: 'line',
            stack: '总量',
            data: []
          },
          {
            name: '分类',
            type: 'line',
            stack: '总量',
            data: []
          },
          {
            name: '商品列表',
            type: 'line',
            stack: '总量',
            data: []
          },
          {
            name: '商品详情',
            type: 'line',
            stack: '总量',
            data: []
          }
        ]
      }
    }
  },
  created() {},
  async mounted() {
    // 基于准备好的dom，初始化echarts实例
    let myChart = echarts.init(document.getElementById('yonghu'))

    const { data } = await this.$axios.get('reports/type/1')
    // console.log(data)
    if (data.meta.status !== 200) {
      return this.$message.error('获取折线图数据失败！')
    }
    let obj1 = JSON.parse(JSON.stringify(this.option1))
    let obj2 = JSON.parse(JSON.stringify(data.data))
    // 使用 es6 的 Object.assign 方法 合并两个对象
    const result = Object.assign(obj1, obj2)

    // console.log(result)
    // 使用刚指定的配置项和数据显示图表。
    this.$nextTick(() => {
      myChart.setOption(result)
    })

    setTimeout(async () => {
      let myChart2 = echarts.init(document.getElementById('dianji'))
      const { data: res } = await this.$axios.get('reports/type/2')
      // console.log(res.data)
      if (res.meta.status !== 200) {
        return this.$message.error('获取折线图2数据失败！')
      }
      // 获取 key
      this.option2.xAxis.data = Object.keys(res.data).reverse()
      // 赋值 value
      let arrList = Object.values(res.data).reverse()
      // console.log(arrList)

      arrList.forEach(item => {
        this.option2.series[0].data.push(item[0].rp2_count)
        this.option2.series[1].data.push(item[1].rp2_count)
        this.option2.series[2].data.push(item[2].rp2_count)
        this.option2.series[3].data.push(item[3].rp2_count)
      })
      // 使用刚指定的配置项和数据显示图表。
      this.$nextTick(() => {
        myChart2.setOption(this.option2)
      })
    }, 2000)
  }
}
</script>

<style scoped>
</style>